<template>
	<!-- 主弹框 -->
	<view class="notice-modal" v-if="visible" @touchmove.stop.prevent>
		<view class="modal-mask" @click="close"></view>
		<view class="modal-content">
			<view class="modal-header">
				<text class="modal-title">创建直播预告</text>
				<button class="create-btn" @click="handleCreate">创建</button>
			</view>

			<view class="form-content">
				<!-- 开播时间 -->
				<view class="form-item" @click="openTimePicker">
					<text class="item-label">开播时间*</text>
					<view class="item-value">
						<text class="time-text" :class="{placeholder: !selectedTime}">
							{{ selectedTime || '请选择' }}
						</text>
						<uni-icons type="right" size="20"></uni-icons>
					</view>
				</view>

				<!-- 预告封面 -->
				<view class="form-item">
					<text class="item-label">预告封面</text>
					<view class="cover-upload" @click="chooseCover">
						<image v-if="coverImage" :src="coverImage" class="cover-image" mode="aspectFill"></image>
						<view v-else class="upload-placeholder">
							<text class="plus-icon">+</text>
						</view>
					</view>
				</view>

				<!-- 直播介绍 -->
				<view class="form-item">
					<text class="item-label">直播介绍</text>
					<textarea v-model="introduction" class="intro-textarea" placeholder="直播介绍可以更好的帮助观众了解您的直播内容"
						maxlength="100"></textarea>
					<text class="word-count">{{ introduction.length }}/100</text>
				</view>
			</view>
		</view>

		<!-- 时间选择器弹框 -->
		<view v-if="showTimePicker" class="time-picker-modal">
			<view class="picker-mask" @click="cancelTimePicker"></view>
			<view class="picker-content">
				<view class="picker-header">
					<text class="picker-title">选择开播时间</text>
				</view>
				<picker-view :value="pickerValue" @change="handleTimeChange" class="time-picker"
					indicator-style="height: 80rpx;">
					<picker-view-column>
						<view v-for="(item,index) in years" :key="index" class="picker-item">{{item}}年</view>
					</picker-view-column>
					<picker-view-column>
						<view v-for="(item,index) in months" :key="index" class="picker-item">{{item}}月</view>
					</picker-view-column>
					<picker-view-column>
						<view v-for="(item,index) in days" :key="index" class="picker-item">{{item}}日</view>
					</picker-view-column>
					<picker-view-column>
						<view v-for="(item,index) in hours" :key="index" class="picker-item">{{item}}时</view>
					</picker-view-column>
					<picker-view-column>
						<view v-for="(item,index) in minutes" :key="index" class="picker-item">{{item}}分</view>
					</picker-view-column>
				</picker-view>
				<view class="picker-buttons">
					<button class="picker-btn cancel" @click="cancelTimePicker">取消</button>
					<button class="picker-btn confirm" @click="confirmTimePicker">确定</button>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	import uniIcons from '@/uni_modules/uni-icons/components/uni-icons/uni-icons.vue'

	export default {
		components: {
			uniIcons
		},
		props: {
			visible: {
				type: Boolean,
				default: false
			}
		},
		data() {
			const now = new Date()
			const currentYear = now.getFullYear()
			const currentMonth = now.getMonth() + 1
			const currentDay = now.getDate()
			const currentHour = now.getHours()
			const currentMinute = now.getMinutes()

			return {
				showTimePicker: false,
				selectedTime: '',
				coverImage: '',
				introduction: '',
				pickerValue: [5, currentMonth - 1, currentDay - 1, currentHour, currentMinute],
				years: Array.from({
					length: 11
				}, (_, i) => currentYear - 5 + i),
				months: Array.from({
					length: 12
				}, (_, i) => i + 1),
				days: Array.from({
					length: 31
				}, (_, i) => i + 1),
				hours: Array.from({
					length: 24
				}, (_, i) => i),
				minutes: Array.from({
					length: 60
				}, (_, i) => i),
				tempTime: ''
			}
		},
		methods: {
			close() {
				this.$emit('close')
			},
			handleCreate() {
				if (!this.selectedTime) {
					uni.showToast({
						title: '请选择开播时间',
						icon: 'none'
					})
					return
				}

				this.$emit('create', {
					time: this.selectedTime,
					cover: this.coverImage,
					introduction: this.introduction
				})
				this.resetForm() // 添加这行，创建成功后重置表单
				this.close()
			},
			// 新增重置表单方法
			resetForm() {
				this.selectedTime = ''
				this.coverImage = ''
				this.introduction = ''
				const now = new Date()
				this.pickerValue = [
					5,
					now.getMonth(),
					now.getDate() - 1,
					now.getHours(),
					now.getMinutes()
				]
			},
			openTimePicker() {
				this.showTimePicker = true
				if (this.selectedTime) {
					try {
						const timeArr = this.selectedTime.match(/(\d+)/g)
						if (timeArr && timeArr.length >= 5) {
							this.pickerValue = [
								this.years.indexOf(parseInt(timeArr[0])) || 5,
								parseInt(timeArr[1]) - 1 || 0,
								parseInt(timeArr[2]) - 1 || 0,
								parseInt(timeArr[3]) || 0,
								parseInt(timeArr[4]) || 0
							]
						}
					} catch (e) {
						console.error('时间解析错误:', e)
					}
				}
			},
			cancelTimePicker() {
				this.showTimePicker = false
			},
			confirmTimePicker() {
				this.selectedTime = this.tempTime || this.formatTime(this.pickerValue)
				this.showTimePicker = false
			},
			handleTimeChange(e) {
				const val = e.detail.value
				this.tempTime = this.formatTime(val)
			},
			formatTime(value) {
				const val = value || this.pickerValue
				return `${this.years[val[0]]}-${this.addZero(this.months[val[1]])}-${this.addZero(this.days[val[2]])} ${this.addZero(this.hours[val[3]])}:${this.addZero(this.minutes[val[4]])}`
			},
			addZero(num) {
				return num < 10 ? `0${num}` : num
			},
			chooseCover() {
				uni.chooseImage({
					count: 1,
					sizeType: ['compressed'],
					sourceType: ['album'],
					success: (res) => {
						this.coverImage = res.tempFilePaths[0]
					},
					fail: (err) => {
						console.error('选择封面失败:', err)
						uni.showToast({
							title: '选择封面失败',
							icon: 'none'
						})
					}
				})
			}
		}
	}
</script>

<style lang="scss">
	.notice-modal {
		position: fixed;
		top: 0;
		left: 0;
		width: 100%;
		height: 100%;
		z-index: 10000;

		.modal-mask {
			position: absolute;
			top: 0;
			left: 0;
			width: 100%;
			height: 100%;
			background-color: rgba(0, 0, 0, 0.5);
		}

		.modal-content {
			position: absolute;
			bottom: 0;
			left: 0;
			width: 100%;
			background-color: #fff;
			border-radius: 24rpx 24rpx 0 0;
			max-height: 80vh;
			overflow-y: auto;
		}

		.modal-header {
			display: flex;
			justify-content: space-between;
			align-items: center;
			padding: 30rpx;
			border-bottom: 1rpx solid #f0f0f0;

			.modal-title {
				font-size: 34rpx;
				font-weight: bold;
				color: #333;
			}

			.create-btn {
				background: none;
				border: none;
				color: #55aaff;
				font-size: 32rpx;
				padding: 0;
				margin: 0;
				line-height: 1;

				&::after {
					border: none;
				}
			}
		}

		.form-content {
			padding: 30rpx;
		}

		.form-item {
			margin-bottom: 40rpx;

			.item-label {
				display: block;
				font-size: 30rpx;
				color: #333;
				margin-bottom: 20rpx;
				font-weight: 500;
			}

			.item-value {
				display: flex;
				align-items: center;
				justify-content: space-between;
				padding: 20rpx 0;
				border-bottom: 1rpx solid #f0f0f0;

				.time-text {
					font-size: 28rpx;
					color: #333;

					&.placeholder {
						color: #999;
					}
				}
			}

			.cover-upload {
				width: 200rpx;
				height: 200rpx;
				background-color: #f7f7f7;
				border-radius: 8rpx;
				display: flex;
				align-items: center;
				justify-content: center;

				.cover-image {
					width: 100%;
					height: 100%;
					border-radius: 8rpx;
				}

				.upload-placeholder {
					text-align: center;

					.plus-icon {
						font-size: 60rpx;
						color: #ccc;
						line-height: 1;
					}
				}
			}

			.intro-textarea {
				width: 100%;
				height: 200rpx;
				background-color: #f7f7f7;
				border-radius: 8rpx;
				padding: 20rpx;
				box-sizing: border-box;
				font-size: 28rpx;
				color: #333;
			}

			.word-count {
				display: block;
				text-align: right;
				font-size: 24rpx;
				color: #999;
				margin-top: 10rpx;
			}
		}

		.time-picker-modal {
			position: fixed;
			top: 0;
			left: 0;
			width: 100%;
			height: 100%;
			z-index: 1001;

			.picker-mask {
				position: absolute;
				top: 0;
				left: 0;
				width: 100%;
				height: 100%;
				background-color: rgba(0, 0, 0, 0.5);
			}

			.picker-content {
				position: absolute;
				bottom: 0;
				left: 0;
				width: 100%;
				background-color: #fff;
				border-radius: 24rpx 24rpx 0 0;
				padding: 20rpx;
			}

			.picker-header {
				text-align: center;
				padding: 20rpx 0;

				.picker-title {
					font-size: 32rpx;
					font-weight: bold;
					color: #333;
				}
			}

			.time-picker {
				height: 400rpx;
			}

			.picker-item {
				height: 80rpx;
				line-height: 80rpx;
				text-align: center;
				font-size: 28rpx;
				color: #333;
			}

			.picker-buttons {
				display: flex;
				justify-content: space-between;
				margin-top: 20rpx;

				.picker-btn {
					flex: 1;
					height: 80rpx;
					line-height: 80rpx;
					text-align: center;
					font-size: 30rpx;
					background: none;
					border: none;
					padding: 0;
					margin: 0;

					&::after {
						border: none;
					}

					&.cancel {
						color: #999;
					}

					&.confirm {
						color: #55aaff;
					}
				}
			}
		}
	}
</style>